package library;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class LibraryTest {
	
	public static void main(String[] args) {	
		LibraryTest l = new LibraryTest();
		l.start();

	}

	BookDao bdao = new BookDao();
	CardDao cdao = new CardDao();
	RecordingDao rdao = new RecordingDao();
	Scanner sc = new Scanner(System.in);
	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");

	void start() {
		while (true) {
			System.out.println("-------------------------------");
			System.out.println("请选择功能:");
			System.out.println("1、进书");
			System.out.println("2、借书卡办理");
			System.out.println("3、借书");
			System.out.println("4、还书");
			System.out.println("5、查询图书借出记录");
			System.out.println("6、查询借书卡借出记录");
			System.out.println("7、显示图书列表（按借出次数排序）");
			System.out.println("0、结束程序");
			System.out.println("-------------------------------");
			String str = sc.nextLine();
			switch (str) {
			case "1":
				input();
				break;
			case "2":
				handle();
				break;
			case "3":
				output();
				break;
			case "4":
				reTurn();
				break;
			case "5":
				bookRecording();
				break;
			case "6":
				cardRecording();
				break;
			case "7":
				display();
				break;
			case "0":
				System.out.println("程序已结束");
				return;
			default:
				System.out.println("输入错误，请重新尝试：");
			}
		}
	}

	/**
	 * 进书
	 */
	void input() {
		while (true) {
			int result = 0;
			BookModel book = new BookModel();
			System.out.println("请输入进书的书名：");
			String s = sc.nextLine();
			
			book.setName(s);
			int res = selName(book);
			
			System.out.println("请输入进书的数目：");
			int n = Integer.parseInt(sc.nextLine());
			book.setNumb(n);
			
			
			if(res==0) {
				book.setLeadcount(0);
				result = bdao.insert(book);
			}else {
				result = bdao.update(book);
			}
			
			
			if(result == 1) {
				System.out.println("进书成功");
			}else {
				System.out.println("进书失败");
			}
			System.out.println("是否还有其他进书需要存入(y/n)");
			String str = sc.nextLine();
			if (str.equalsIgnoreCase("n")) {
				break;
			}
		}
		
	}
	
	int selName(BookModel book) {//查询是否存在该书
		List<BookModel> result = bdao.selectList(book);
		return result.size();
	}

	/**
	 * 借书卡办理
	 */
	void handle() {
		CardModel card = new CardModel();
		String id = "";

		while (true) {
			System.out.println("请输入卡号：");
			id = sc.nextLine();
			System.out.println("请输入密码：");
			String password = sc.nextLine();
			if (duplicate(id)==0) {
				card.setId(id);
				card.setPassword(password);
				cdao.insert(card);
				System.out.println("卡号已激活！");
				break;
			} else {
				System.out.println("该卡号已存在，请重新输入");
			}
		}

	}

	/**
	 * 卡号不存在返回0
	 * 
	 * @param id
	 * @return
	 */
	int duplicate(String id) {
		CardModel card = new CardModel();
		card.setId(id);
		List<CardModel> result= cdao.selectList(card);
		return result.size();
	}

	/**
	 * 借书
	 */
	void output() {
		System.out.println("请输入想要借书卡卡号：");
		String id = sc.nextLine();
		System.out.println("请输入想要借的图书书名：");
		String name = sc.nextLine();
		BookModel model = new BookModel();
		int s = selName(model);
		if (s == 0) {
			System.out.println("该书不存在！");
		} else {
			BookModel book = new BookModel();
			book.setName(name);
			int n = bdao.selectList(book).get(0).getNumb();
			int m = bdao.selectList(book).get(0).getLeadcount();
			if (n != 0) {
				book.setNumb(n - 1);
				book.setLeadcount(m + 1);
				System.out.println("已查找到该书！");
				bdao.update(book);
				recording(name, id);

			} else {
				System.out.println("该书在中图书馆已无库存，请下次再来！");

			}
		}
	}

	/**
	 * 借书卡借书记录
	 * 
	 * @param retname
	 */
	void recording(String retname,String strid) {
		RecordingModel rec = new RecordingModel();
		Date d = new Date();
		Timestamp date = new Timestamp(d.getTime());
		rec.setStatus(0); // 借书
		rec.setDate(date);
		rec.setCid(strid);
		rec.setName(retname);
		int result = rdao.insert(rec);
		if(result==1) {
			System.out.println("借书成功，已为您记录！");
		}else {
			System.out.println("借书失败！");
		}
		
	}

	/**
	 * 查询图书借出记录
	 */
	void bookRecording() {
		boolean t = false;
		RecordingModel model =new RecordingModel();
		model.setStatus(0);
		List<RecordingModel> listRecording = rdao.selectList(model);
		for (RecordingModel rec : listRecording) {
			String strdate = fmt.format(rec.getDate());
			System.out.println("借书日期：" + strdate);
			System.out.println("借书卡号：" + rec.getCid());
			System.out.println("书名：" + rec.getName());
			t = true;
		}
		if (t) {
			System.out.println("以上便是全部图书外借信息！");
		} else {
			System.out.println("暂未有过书籍外借");
		}
	}

	/**
	 * 查询借书卡借书记录
	 */
	void cardRecording() {
		CardModel card = new CardModel();
		String id = "";
		while (true) {
			System.out.println("请输入想要查询的卡号：");
			 id = sc.nextLine();
			if (duplicate(id)==0) {
				System.out.println("该卡号暂未注册，请重新输入");
			} else {
				card.setId(id);
				break;
			}
				
		}
		
		while (true) {
			System.out.println("请输入密码：");
			String password = sc.nextLine();
			List<CardModel> listCard = cdao.selectList(card);	
				if (listCard.get(0).getPassword().equals(password)) {
					break;
				}else {
					System.out.println("密码输入错误，请重新尝试");
			}
			
		}
		RecordingModel recod = new RecordingModel();
		recod.setCid(id);
		List<RecordingModel> listRecording = rdao.selectList(recod);
		for (RecordingModel rec : listRecording) {
			System.out.println("书名：" + rec.getName());
			String strdate = fmt.format(rec.getDate());
			if (rec.getStatus() == 0) {
				System.out.println("借书日期：" + strdate);
			} else {
				System.out.println("还书日期：" + strdate);
			}

		}
	}

	/**
	 * 还书
	 */
	void reTurn() {
		System.out.println("请输入要归还图书的书名：");
		String name = sc.nextLine();
		BookModel model =new BookModel();
		model.setName(name);
		List<BookModel> listBook = bdao.selectList(model);
		int numb = listBook.get(0).getNumb();
		model.setNumb(numb+1);
		int result = bdao.update(model);
		judge(name,result);
	}

	/**
	 * 确认还书的卡号
	 */
	void judge(String bname, int result) {
		System.out.println("请输入借书卡卡号：");
		String id = "";
		while (true) {
			id = sc.nextLine();
			if (duplicate(id) != 0) {
				break;
			}
			System.out.println("并未查找到该卡号，请重新输入！");
		}
		if (result == 1) {
			System.out.println("还书成功！");
			RecordingModel rec = new RecordingModel();
			Date d = new Date();
			Timestamp date = new Timestamp(d.getTime());
			rec.setStatus(1);
			rec.setCid(id);
			rec.setName(bname);
			rec.setDate(date);
			rdao.insert(rec);
		}

	}

	/**
	 * 按借出次数排序
	 * @return 
	 */
	List<BookModel> Sequence() {
		BookModel model = new BookModel();
		model.setOrderby("t");
		List<BookModel> listBook = bdao.appendOrderby(model);
		return listBook;
	}

	/**
	 * 按借出次数排序
	 */
	void display() {
		List<BookModel> listBook = Sequence();
		for (BookModel book : listBook) {
			System.out.println("书名：" + book.getName());
			System.out.println("数目：" + book.getNumb());
			System.out.println("被借出次数：" + book.getLeadcount());
		}
		System.out.println("已完成输出");
	}
}